<template>
    <el-dialog v-model="visible" title="新建请假申请" width="50%" style="padding-right:50px;" >
        <el-form label-width="100px">

            <el-form-item label="申请类型：">
                <el-select v-model="data.type" placeholder="请选择申请类型" style="width:100%">
                    <el-option
                        v-for="item in TypeOptions"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    />
                </el-select>
            </el-form-item>

            <el-form-item label="详情：">
                <el-input v-model="data.lcause" type="textarea" />
            </el-form-item>

            <el-form-item label="开始时间：">
                <el-date-picker v-model="data.lStart" type="datetime" format="YYYY-MM-DD HH:mm"
                                value-format="YYYY-MM-DD HH:mm" data-format="YYYY-MM-DD" time-format="HH:mm"
                                style="width:100%"
                />
            </el-form-item>

            <el-form-item label="结束时间：">
                <el-date-picker v-model="data.lEnd" type="datetime" format="YYYY-MM-DD HH:mm"
                                value-format="YYYY-MM-DD HH:mm" data-format="YYYY-MM-DD" time-format="HH:mm"
                                style="width:100%"
                />
            </el-form-item>

        </el-form>

        <template #footer >
            <el-button type="primary" @click="exec" >确定</el-button>
            <el-button @click="visible=false" >取消</el-button>
        </template>

    </el-dialog>
</template>
<script setup>
import {ref,reactive} from 'vue'
import req from '@/request'
import {ElMessage} from 'element-plus'

const TypeOptions = [
  { value: 1, label: '请假' },
  { value: 2, label: '工作汇报' },
  { value: 3, label: '出差' }
]

const emit = defineEmits(['ok']);

const visible = ref(false);

const open = ()=>{
    visible.value = true;
}

const data = reactive({
    lcause:'',
    lStart:'',
    lEnd:'',
    type:1
});

const exec = async()=>{
    try {
        // Prepare the data to match the API expected format
        const requestData = {
            lcause: data.lcause,
            lStart: data.lStart,
            lEnd: data.lEnd,
            type: data.type
        };
        
        await req.post('/leave/apprev', requestData);
        emit('ok');
        // Reset form
        data.lcause = '';
        data.lStart = '';
        data.lEnd = '';
        data.type = 1;
        visible.value = false;
        ElMessage.success('申请提交成功！');
    } catch (error) {
        ElMessage.error('提交失败: ' + (error.response?.data?.message || error.message));
    }
}

defineExpose({
    open
});
</script>
<style scoped>
</style>